package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.domain.ResultPage;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpServiceImpl empService;

    @GetMapping
    public Result getAll(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         Integer gender,
                         String name,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        ResultPage resultPage = empService.getAll(page, pageSize, gender, name, begin, end);
        return Result.success(resultPage);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") Long[] ids) {
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Emp emp) {
        empService.add(emp);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getOneById(@PathVariable("id") Long id) {
    Emp emp=empService.getOneById(id);
    return  Result.success(emp);
    }
}
